package com.xii.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.xii.mapper.OrderSettingMapper;
import com.xii.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void addOrderSetting(List<OrderSetting> orderSettings) {
        for (OrderSetting orderSetting : orderSettings) {
            orderSettingMapper.addOrderSetting(orderSetting);
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String beginTime = date+"-1";
        String endTime = date+"-31";
        List<OrderSetting> orderSettingList = orderSettingMapper.getOrderSettingByMonth(beginTime,endTime);
        List<Map> maps = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            HashMap<String, Object> ordersettingmap = new HashMap<>();
            ordersettingmap.put("date",orderSetting.getOrderDate().getDate());
            ordersettingmap.put("number",orderSetting.getNumber());
            ordersettingmap.put("reservations",orderSetting.getReservations());
            maps.add(ordersettingmap);
        }
        return maps;
    }

    /**
     * 修改预约
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //查询当前日期是否有预约
        OrderSetting orderSettingdb = orderSettingMapper.getOrderSettingByDate(orderSetting.getOrderDate());
        //判断是否有数据
        if(orderSettingdb != null){
            //修改
            orderSettingMapper.updateOrderSettingByDate(orderSetting);
        }else{
            orderSettingMapper.addOrderSetting(orderSetting);
        }
    }
}
